//
//  Development_Map_Vc.swift
//  LocaLink
//
//  Created by Icy on 2017/1/16.
//  Copyright © 2017年 Icy. All rights reserved.
//

/*
 owner:cy
 update:2017年01月16日21:27:55
 info: 社区地图选择界面
 */
import RxSwift
import MapKit
import GoogleMaps
import UtilCore

class Development_Map_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    
    @IBOutlet weak var content_V: UIView!
    
//    @IBOutlet weak var development_MapV: MKMapView!
    /*----------------------------   自定义属性区域    ----------------------------*/
    @IBOutlet weak var location_Btn: UIButton!
    let locationManager = CLLocationManager()
    var myMapView:GMSMapView?
    ///是否从登陆界面进入
    var isModal:Bool = false
    
    var manage_Vm:Development_Map_Vm?
    
    var developmentList:[DevelopmentModel] = []
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let identifier = segue.identifier { // 检查是否 nil
            switch identifier {
            case "ToListVc":
                if let listVc = segue.destination as? Development_List_Vc {
                    listVc.isModal = self.isModal
                    listVc.developmentList = self.developmentList
                }
            default: break
            }
        }
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.navigationController?.navigationBar.isHidden = true
         self.locationManager.requestWhenInUseAuthorization()
        locationManager.delegate = self
        let camera = GMSCameraPosition.camera(withLatitude: 13.727212, longitude: 100.540639, zoom: 15.0)
        let mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: content_V.frame.size.width, height: content_V.frame.size.height), camera: camera)
      
        mapView.delegate = self
        
        mapView.isMyLocationEnabled = true
        mapView.settings.compassButton = true
        mapView.settings.indoorPicker = true
        mapView.settings.myLocationButton = true
        
        self.content_V.addSubview(mapView)
        self.myMapView = mapView
        startLocation()
        addMarket()
    }
    func addMarket(){
//        CLLocationCoordinate2D curCoordinate2D=curLocation.coordinate;
//        self.marker=[GMSMarker markerWithPosition: curCoordinate2D];
//        self.marker.title=@"标记的点"
//        self.marker.icon=[GMSMarker markerImageWithColor:[UIColor redColor]];
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: 37.87643, longitude: 112.54)
        marker.title = "标记的点"
        marker.icon = UIImage(named: "tab-selected-setting")
        marker.map = self.myMapView

        
    }
    ///开始定位
    func startLocation() {
        let locationManager = CLLocationManager()
        locationManager.delegate = self
        
        locationManager.desiredAccuracy =  kCLLocationAccuracyBest
        locationManager.distanceFilter = 10000
        locationManager.startUpdatingLocation()
        if let center =  locationManager.location?.coordinate {
            print(center)
            self.myMapView?.camera = GMSCameraPosition(target:center, zoom:15,bearing:0, viewingAngle:0)
        }
        
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
        self.location_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                self.startLocation()
                
            })
            .disposed(by: disposeBag)

        self.manage_Vm = Development_Map_Vm(refreshTriger: rx.sentMessage(#selector(Development_Map_Vc.viewWillAppear)).map { _ in ("test","123123") })
        self.manage_Vm?.error.bindTo(self.view.rx_error).addDisposableTo(disposeBag)
        self.manage_Vm?.developmentList
            .subscribe(onNext: { [unowned self] ( list ) in
                self.developmentList = list
            })
            .disposed(by: disposeBag)
    }
    
    /**
     自定义leftBarButtonItem
     */
    override func customLeftBarButtonItem()  {
        
    }
    
    
    
}
extension Development_Map_Vc :CLLocationManagerDelegate{
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
//        CLGeocoder.reverseGeocodeLocation(manager.location)
        
        self.myMapView?.camera = GMSCameraPosition(target:(manager.location?.coordinate)!, zoom:15,bearing:0, viewingAngle:0)
        locationManager.stopUpdatingLocation()
    }
    
}


extension Development_Map_Vc :MKMapViewDelegate{
    
}

extension Development_Map_Vc : GMSMapViewDelegate{
    func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker){
        
    }
}

